package other.xsy.passlys;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEvent;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventRequest;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventResponse;
import com.rkhd.platform.sdk.data.model.Booking__c;
import com.rkhd.platform.sdk.data.model.TeamMember;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.BatchOperateResult;
import other.xsy.passlys.util.HCXObjectService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 订单下的Booking，添加销售为团队成员
 * @Author: 黎炎松
 * @Date: 2024/4/19 16:48
 */
public class BookingAddTeamByOrderAutoFlow implements AutoFlowEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "订单下的Booking，添加销售为团队成员 ==> ";

    @Override
    public AutoFlowEventResponse execute(AutoFlowEventRequest request) throws ScriptBusinessException {
        logger.debug(LOGGER_MSG + " 触发开始 ");
        logger.info("ObjectApiKey:" + request.getEntityApiKey() + " , DataId:" + request.getDataId() + " , ProcessInstanceId:" + request.getProcessInstanceId());
        logger.info(request.toString());
        AutoFlowEventResponse response = new AutoFlowEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        bookingAddTeamByOrder(request.getDataId());
        return response;
    }

    public static void main(String[] args) throws ApiEntityServiceException {
        BookingAddTeamByOrderAutoFlow bookingAddTeamAutoFlow = new BookingAddTeamByOrderAutoFlow();
        bookingAddTeamAutoFlow.bookingAddTeamByOrder(3243244349902176L);
    }


    /**
     *  根据Booking自动创建，添加销售为团队成员
     * @param orderId
     */
    private void bookingAddTeamByOrder(Long orderId) {
        try {
            logger.debug(LOGGER_MSG + " 处理开始: " + orderId);

            // 查询订单下的 booking
            String sql = " select id,bookingSales__c,ownerId from booking__c where order__c  =" + orderId;
            List<Booking__c> bookingList = hcxObjectService.query(sql, Booking__c.class);

            // 循环处理
            ArrayList<TeamMember> teamMemberList = new ArrayList<>();
            for (Booking__c booking : bookingList) {
                // 添加 销售 为团队成员
                TeamMember teamMember = new TeamMember();
                teamMember.setEntityType(11010064400001L);
                teamMember.setOwnerFlag(2); // 1: 有修改权限 2: 只可以查看
                teamMember.setUserId(booking.getBookingSales__c()); // 销售
                teamMember.setRecordFrom_data(booking.getId());
                teamMember.setRecordFrom(3226444676959521L);
                teamMemberList.add(teamMember);

                // 添加 所有人 为团队成员
                if (!Objects.equals(booking.getBookingSales__c(), booking.getOwnerId())) {
                    TeamMember newTeamMember = JSONObject.parseObject(teamMember.toString(), TeamMember.class);
                    newTeamMember.setUserId(booking.getOwnerId()); // 所有人
                    teamMemberList.add(newTeamMember);
                }
            }

            BatchOperateResult batchOperateResult = hcxObjectService.batchInsert(teamMemberList);
            logger.debug(LOGGER_MSG + " 批量添加团队成员: " + batchOperateResult);
            if (!batchOperateResult.getSuccess()) {
                logger.debug(LOGGER_MSG + " 批量添加团队成员失败: " + batchOperateResult.getErrorMessage());
            }

        } catch (ApiEntityServiceException e) {
            logger.debug(LOGGER_MSG + " 处理异常");
        }
    }

}
